<?php
namespace app\model;
use think\Model;

class Department extends Model
{
    public function getInfoByWorkNum($num){
        try {
            $data = $this->where('work_num',$num)->find();
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
        }
    }

    public function getAll($list_rows,bool $simple = false,$config,$post){
        $select_post = array_diff_key ( $post, ['list_rows'=>-1,'page'=>-1] );
        try {
            $data = $this->where($select_post)->paginate($list_rows,$simple,$config)->toArray();
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
        }
    }

    public function getAllWorkNum(){
        try {
            $data = $this->withAttr('work_num')->select();
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
            //throw $th;
        }
    }
    
    public function createDepartment($department){
        try {
            $data = $this->insert(['department' => $department]);
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
            //throw $th;
        }
    }

    public function deleteDepartment($id){
        try {
            // $status = 6 - $status;
            // $flag = 0;
            // if($status ==='已通过'){
            //     $flag = 1;
            // }else{
            //     return[false,$status];
            // }
            // return $status;
            $data = $this->where('id', $id)->delete();
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
            //throw $th;
        }
    }
}
